/**
 * 
 */
package smallChanges.statements;

import java.util.LinkedList;
import java.util.List;

/**
 * @author jspacco
 *
 */
public class ParsedSourceFile
{
    private String filename;
    /** Top-level classes, each of which may contain inner classes.
     * It's possible (though extremely poor) to declare multiple 
     * top-level classes in the same file.
     */
    private List<ClassDeclaration> topLevelClasses=new LinkedList<ClassDeclaration>();;
    private StatementList importStatements=StatementList.EMPTY_STATEMENT_LIST;
    
    public List<ClassDeclaration> flattenClasses() {
        List<ClassDeclaration> list=new LinkedList<ClassDeclaration>();
        for (ClassDeclaration c : topLevelClasses) {
            flattenClassesHelper(c, list);
        }
        return list;
    }
    private static List<ClassDeclaration> flattenClassesHelper(
            ClassDeclaration c,
            List<ClassDeclaration> list)
    {
        list.add(c);
        if (c.getInnerClasses()!=null) {
            for (ClassDeclaration inner : c.getInnerClasses()) {
                flattenClassesHelper(inner, list);
            }
        }
        return list;
    }
    public void addTopLevelClass(ClassDeclaration classDeclaration)
    {
        topLevelClasses.add(classDeclaration);
        
    }
    public void addImportStatement(ImportStatement stmt)
    {
        if (importStatements==StatementList.EMPTY_STATEMENT_LIST) {
            importStatements=new StatementList(stmt.allTokensInClass);
            importStatements.setBeginLine(stmt.getBeginLine());
            importStatements.setBeginColumn(stmt.getBeginColumn());
        }
        importStatements.addStatement(stmt);
        importStatements.setEndLine(stmt.getEndLine());
        importStatements.setEndColumn(stmt.getEndColumn());
    }
    public List<ClassDeclaration> getTopLevelClassDeclarations()
    {
        return topLevelClasses;
    }
    public StatementList getImportStatements()
    {
        return importStatements;
    }
    public String getFilename()
    {
        return filename;
    }
    public void setFilename(String filename)
    {
        this.filename = filename;
    }
}
